package com.sunlands.zlcx.usercenter.repository;

import com.sunlands.zlcx.usercenter.domain.InviteCodeDO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;

import javax.persistence.LockModeType;
import java.util.List;

/**
 * @author shixiaopeng
 */
public interface InviteCodeRepository extends JpaRepository<InviteCodeDO, Long>, JpaSpecificationExecutor<InviteCodeDO> {

    @Query(value = "select * from tz_invite_code p where p.user_id =?1 and  date(p.create_time)=?2", nativeQuery = true)
    List<InviteCodeDO> findAllByUserIdAndToday(Long userId, String today);

    @Query(value = "select count(*) from tz_invite_code p where p.user_id =?1 and  date(p.create_time)=?2", nativeQuery = true)
    long countByUserIdAndCreateTime(Long userId, String today);

    @Query(value = "select * from tz_invite_code p where p.code =?1 and  date(p.create_time)=?2", nativeQuery = true)
    InviteCodeDO findByCodeAndToday(String code, String today);

    InviteCodeDO findByCode(String code);

    InviteCodeDO findByInviteUserId(Long inviteUserId);

    @Lock(value = LockModeType.PESSIMISTIC_WRITE)
    InviteCodeDO findById(Long id);
}
